class SettingController < ApplicationController
  #Solo puede ser accesados por el administrador
  before_filter :admin_required, :only => [ :index, :list, :def_changes ]

  #Muestra todos los parametros configurables por el administrador
  def index
    list
    render :action=>'list'
  end

  def list
    @title = "Opciones del Sistema"
    @settings = SystemSetting.find(:all)
  end

  def save_changes
    if request.post?
      @error = []
      if(params[:smtp_auth_passwd] != params[:smtp_auth_passwd2])
        flash[:error] = "Los dos passwords ingresados son diferentes"
        redirect_to :action =>'list' and return
      end
      @settings = SystemSetting.find(:all)
      @cambios = [params[:admin_mail],
                  params[:site_base],
                  params[:site_host],
                  params[:site_name],
                  params[:max_events_per_usert],
                  params[:max_file_size],
                  params[:max_students_per_course],
                  params[:max_instructors_per_course],
                  params[:remove_course_after_seconds],
                  params[:smtp_server],
                  params[:smtp_port],
                  params[:smpt_auth_method],
                  params[:smpt_auth_login],
                  params[:smtp_auth_passwd]]
      
=begin       @settings.each_index { |s|
        param = @settings[s].name.gsub("_","-")
        @settings[s].value = params[@settings[s].name.gsub("_", "-")]
        @settings[s].save
      }
=end
    for i in 0...@settings.size
      @settings[i].value = @cambios[i]
      @settings[i].save
    end

    else
      flash[:error] = "Error de POST"
    end
    redirect_to :controller=> 'account', :action => 'index'
  end
end
